home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / pgen2 / grammar.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.4 KB  |  124 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """This module defines the data structures used to represent a grammar.
  5.  
  6. These are a bit arcane because they are derived from the data
  7. structures used by Python's 'pgen' parser generator.
  8.  
  9. There's also a table here mapping operators to their names in the
  10. token module; the Python tokenize module reports all operators as the
  11. fallback token code OP, but the parser needs the actual token code.
  12.  
  13. """
  14. import pickle
  15. from  import token, tokenize
  16.  
  17. class Grammar(object):
  18.     """Pgen parsing tables tables conversion class.
  19.  
  20.     Once initialized, this class supplies the grammar tables for the
  21.     parsing engine implemented by parse.py.  The parsing engine
  22.     accesses the instance variables directly.  The class here does not
  23.     provide initialization of the tables; several subclasses exist to
  24.     do this (see the conv and pgen modules).
  25.  
  26.     The load() method reads the tables from a pickle file, which is
  27.     much faster than the other ways offered by subclasses.  The pickle
  28.     file is written by calling dump() (after loading the grammar
  29.     tables using a subclass).  The report() method prints a readable
  30.     representation of the tables to stdout, for debugging.
  31.  
  32.     The instance variables are as follows:
  33.  
  34.     symbol2number -- a dict mapping symbol names to numbers.  Symbol
  35.                      numbers are always 256 or higher, to distinguish
  36.                      them from token numbers, which are between 0 and
  37.                      255 (inclusive).
  38.  
  39.     number2symbol -- a dict mapping numbers to symbol names;
  40.                      these two are each other's inverse.
  41.  
  42.     states        -- a list of DFAs, where each DFA is a list of
  43.                      states, each state is is a list of arcs, and each
  44.                      arc is a (i, j) pair where i is a label and j is
  45.                      a state number.  The DFA number is the index into
  46.                      this list.  (This name is slightly confusing.)
  47.                      Final states are represented by a special arc of
  48.                      the form (0, j) where j is its own state number.
  49.  
  50.     dfas          -- a dict mapping symbol numbers to (DFA, first)
  51.                      pairs, where DFA is an item from the states list
  52.                      above, and first is a set of tokens that can
  53.                      begin this grammar rule (represented by a dict
  54.                      whose values are always 1).
  55.  
  56.     labels        -- a list of (x, y) pairs where x is either a token
  57.                      number or a symbol number, and y is either None
  58.                      or a string; the strings are keywords.  The label
  59.                      number is the index in this list; label numbers
  60.                      are used to mark state transitions (arcs) in the
  61.                      DFAs.
  62.  
  63.     start         -- the number of the grammar's start symbol.
  64.  
  65.     keywords      -- a dict mapping keyword strings to arc labels.
  66.  
  67.     tokens        -- a dict mapping token numbers to arc labels.
  68.  
  69.     """
  70.     
  71.     def __init__(self):
  72.         self.symbol2number = { }
  73.         self.number2symbol = { }
  74.         self.states = []
  75.         self.dfas = { }
  76.         self.labels = [
  77.             (0, 'EMPTY')]
  78.         self.keywords = { }
  79.         self.tokens = { }
  80.         self.symbol2label = { }
  81.         self.start = 256
  82.  
  83.     
  84.     def dump(self, filename):
  85.         '''Dump the grammar tables to a pickle file.'''
  86.         f = open(filename, 'wb')
  87.         pickle.dump(self.__dict__, f, 2)
  88.         f.close()
  89.  
  90.     
  91.     def load(self, filename):
  92.         '''Load the grammar tables from a pickle file.'''
  93.         f = open(filename, 'rb')
  94.         d = pickle.load(f)
  95.         f.close()
  96.         self.__dict__.update(d)
  97.  
  98.     
  99.     def report(self):
  100.         '''Dump the grammar tables to standard output, for debugging.'''
  101.         pprint = pprint
  102.         import pprint
  103.         print 's2n'
  104.         pprint(self.symbol2number)
  105.         print 'n2s'
  106.         pprint(self.number2symbol)
  107.         print 'states'
  108.         pprint(self.states)
  109.         print 'dfas'
  110.         pprint(self.dfas)
  111.         print 'labels'
  112.         pprint(self.labels)
  113.         print 'start', self.start
  114.  
  115.  
  116. opmap_raw = '\n( LPAR\n) RPAR\n[ LSQB\n] RSQB\n: COLON\n, COMMA\n; SEMI\n+ PLUS\n- MINUS\n* STAR\n/ SLASH\n| VBAR\n& AMPER\n< LESS\n> GREATER\n= EQUAL\n. DOT\n% PERCENT\n` BACKQUOTE\n{ LBRACE\n} RBRACE\n@ AT\n== EQEQUAL\n!= NOTEQUAL\n<> NOTEQUAL\n<= LESSEQUAL\n>= GREATEREQUAL\n~ TILDE\n^ CIRCUMFLEX\n<< LEFTSHIFT\n>> RIGHTSHIFT\n** DOUBLESTAR\n+= PLUSEQUAL\n-= MINEQUAL\n*= STAREQUAL\n/= SLASHEQUAL\n%= PERCENTEQUAL\n&= AMPEREQUAL\n|= VBAREQUAL\n^= CIRCUMFLEXEQUAL\n<<= LEFTSHIFTEQUAL\n>>= RIGHTSHIFTEQUAL\n**= DOUBLESTAREQUAL\n// DOUBLESLASH\n//= DOUBLESLASHEQUAL\n-> RARROW\n'
  117. opmap = { }
  118. for line in opmap_raw.splitlines():
  119.     if line:
  120.         (op, name) = line.split()
  121.         opmap[op] = getattr(token, name)
  122.         continue
  123.  
  124.